/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pagoda.util.scripting;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author kevin
 */
public class PrototypeBindings implements Prototype {

    private Prototype _proto;
    private Map<String, Object> _scopeVariables
            = new HashMap<String, Object>();
    
    public Prototype getPrototype() {
        return _proto;
    }

    public Prototype setPrototype(Prototype proto) {
        _proto = proto;
        return this;
    }

    public int size() {
        int s = _scopeVariables.size();
        if(_proto != null)
            s += _proto.size();
        return s;
    }

    public boolean isEmpty() {
        boolean e = _scopeVariables.isEmpty();
        if(_proto != null)
            e &= _proto.isEmpty();
        return e;
    }

    public boolean containsKey(Object key) {
        boolean sc = _scopeVariables.containsKey(key);
        if(sc)
            return true;
        if(_proto != null)
            return _proto.containsKey(key);
        return false;
    }

    public boolean containsValue(Object value) {
        boolean sc = _scopeVariables.containsValue(value);
        if(sc)
            return true;
        if(_proto != null)
            return _proto.containsValue(value);
        return false;
    }

    public Object get(Object key) {
        if(_scopeVariables.containsKey(key))
            return _scopeVariables.get(key);
        if(_proto != null)
            return _proto.get(key);
        return null;
    }

    public Object put(String key, Object value) {
        return _scopeVariables.put(key, value);
    }

    public Object remove(Object key) {
        if(_scopeVariables.containsKey(key))
            return _scopeVariables.remove(key);
        if(_proto != null)
            return _proto.remove(key);
        return null;
    }

    public void putAll(Map<? extends String, ? extends Object> m) {
        _scopeVariables.putAll(m);
    }

    public void clear() {
        _scopeVariables.clear();
        if(_proto != null)
            _proto.clear();
    }

    public Set<String> keySet() {
        Set<String> ks = _scopeVariables.keySet();
        if(_proto != null)
            ks.addAll(_proto.keySet());
        return ks;
    }

    public Collection<Object> values() {
        Collection<Object> vals = _scopeVariables.values();
        if(_proto != null)
            vals.addAll(_proto.values());
        return vals;
    }

    public Set<Entry<String, Object>> entrySet() {
        Set<Entry<String, Object>> es = _scopeVariables.entrySet();
        if(_proto != null)
            es.addAll(_proto.entrySet());
        return es;
    }

}
